(function($) {

	var zp = {
		init: function(obj, pageinit) {
			return(function() {
				zp.addhtml(obj, pageinit);
				zp.bindEvent(obj, pageinit);
			}());
		},
		addhtml: function(obj, pageinit) {
			return(function() {
				obj.empty();
				if(pageinit.shownum < 5) {
					pageinit.shownum = 5;
				}
				if(pageinit.pageNum < pageinit.shownum) {
					pageinit.shownum = pageinit.pageNum;
				}
				var numshow = pageinit.shownum - 4;
				var numbefore = parseInt((pageinit.shownum - 5) / 2);
				if((pageinit.shownum - 5) % 2 > 0) {
					var numafter = numbefore + 1;
				} else {
					var numafter = numbefore;
				}
				/*上一页*/
				if(pageinit.current > 1) {
					obj.append('<a href="javascript:;" class="prebtn">上一页</a>');
				} else {
					obj.remove('.prevPage');
					//可以隐藏第一页
					obj.append('<span class="disabled">上一页</span>');
				}
				/*中间页*/
				if(pageinit.current > 4 && pageinit.pageNum > pageinit.shownum) {
					///动态出现尾页(两边省略)
					obj.append('<a href="javascript:;" class="zxfPagenum">' + 1 + '</a>');
					obj.append('<a href="javascript:;" class="zxfPagenum">' + 2 + '</a>');
					obj.append('<span>...</span>');
				}
				if(pageinit.current > 4 && pageinit.current < pageinit.pageNum - numshow && pageinit.pageNum > pageinit.shownum) {
					var start = pageinit.current - numbefore,
						end = pageinit.current + numafter;
				} else if(pageinit.current > 4 && pageinit.current >= pageinit.pageNum - numshow && pageinit.pageNum > pageinit.shownum) {
					var start = pageinit.pageNum - numshow,
						end = pageinit.pageNum;
				} else {
					if(pageinit.pageNum <= pageinit.shownum) {
						var start = 1,
							end = pageinit.shownum;
					} else {
						var start = 1,
							end = pageinit.shownum - 1;
					}
				}

				//手写(凡是有“手写”的都是为了跳转按钮而生)
				if(end > pageinit.pageNum) {
					var endLen = end.length;
					//这里点击跳转按钮后莫名地拼接了一个1，所以要去掉
					end = end.substring(0, endLen - 1);
					end++;

					//当shownum=9时(也就是中间显现5个)，这里还有++一次
					end++;
				}
				//手写

				for(; start <= end; start++) {
					if(start <= pageinit.pageNum && start >= 1) {
						if(start == pageinit.current) {
							obj.append('<span class="zxfPagenum ' + pageinit.activepage + '">' + start + '</span>');
						} else if(start == pageinit.current + 1) {
							obj.append('<a href="javascript:;" class="zxfPagenum ' + pageinit.activepaf + '">' + start + '</a>');
						} else {
							obj.append('<a href="javascript:;" class="zxfPagenum">' + start + '</a>');
						}
					}
				}

				if(end < pageinit.pageNum) {

					obj.append('<span>...</span>');

					//手写 动态出现尾页(记住调用的时候要把shownum=9,这样中间就会显示5个)
					obj.append('<a href="javascript:;" class="zxfPagenum">' + (pageinit.pageNum - 1) + '</a>');
					obj.append('<a href="javascript:;" class="zxfPagenum">' + pageinit.pageNum + '</a>');
					//手写
				}

				/*下一页*/
				if(pageinit.current >= pageinit.pageNum) {
					obj.remove('.nextbtn');

					//隐藏下一页
					obj.append('<span class="disabled">下一页</span>');
				} else {
					obj.append('<a href="javascript:;" class="nextbtn">下一页</a>');
				}

				/*尾部*/
				obj.append('<span style="margin-left:8px">' + '共' + '<b>' + pageinit.pageNum + '</b>' + '页，' + '</span>');
				obj.append('<span>' + '跳到第' + '<input type="text" class="zxfinput" style="text-indent:0"  onkeyup="keyNum(this)" onafterpaste="keyNum(this)" value="' + pageinit.current + '"/>' + '页' + '</span>');
				obj.append('<span class="zxfokbtn">' + '确定' + '</span>');

			}());
		},
		bindEvent: function(obj, pageinit) {
			return(function() {
				//注销事件
				obj.off("click");
				obj.off("keydown");

				obj.on("click", "a.prebtn", function() {
					currentPage = parseInt(obj.children("span.current").text());
					currentPage = currentPage - 1;
					var current = $.extend(pageinit, {
						"current": currentPage
					});

					//Json
					qPage();

					zp.addhtml(obj, current);
					if(typeof(pageinit.backfun) == "function") {
						pageinit.backfun(current);
					}
				});

				obj.on("click", "a.zxfPagenum", function() {
					currentPage = parseInt($(this).text());
					var current = $.extend(pageinit, {
						"current": currentPage
					});

					//Json
					qPage();

					zp.addhtml(obj, current);
					if(typeof(pageinit.backfun) == "function") {
						pageinit.backfun(current);
					}
				});

				obj.on("click", "a.nextbtn", function() {
					currentPage = parseInt(obj.children("span.current").text());
					currentPage = currentPage + 1;
					var current = $.extend(pageinit, {
						"current": currentPage
					});

					//Json
					qPage();

					zp.addhtml(obj, current);
					if(typeof(pageinit.backfun) == "function") {
						pageinit.backfun(current);
					}
				});

				//跳转页
				obj.on("click", "span.zxfokbtn", function() {

					currentPage = $("input.zxfinput").val();

					//手写 限定最大最小页
					if(currentPage > pageinit.pageNum) {
						currentPage = pageinit.pageNum;
					} else if(currentPage == "" || currentPage == undefined || currentPage == null || currentPage == 0) {
						currentPage = 1;
					}
					//手写 限定最大最小页
					
					var current = $.extend(pageinit, {
						"current": currentPage
					});

					//Json
					qPage();

					zp.addhtml(obj, current);
					if(typeof(pageinit.backfun) == "function") {
						pageinit.backfun(current);
					}
				});

				obj.on("keydown", "input.zxfinput", function() {
					if(event.keyCode == "13") {
						$(".zxfokbtn").click();
					}
				});
			}());
		}
	}

	$.fn.createPage = function(options) {
		var pageinit = $.extend({
			pageNum: 15,
			current: 1,
			shownum: 1,
			activepage: "current",
			activepaf: "nextpage",
			backfun: function() {}
		}, options);
		zp.init(this, pageinit);
	}
}(jQuery));

function qPage() {
	if(page_type == "category_list") {
		getList(currentPage, everyPage, sourceId, categoryName, keyWord);
	}
}